package com.pig4cloud.pig.admin.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.admin.api.entity.BsVipManageEntity;
import com.pig4cloud.pig.admin.service.BsVipManageService;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.annotation.Inner;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 会员管理
 *
 * @author pig
 * @date 2025-04-03 17:43:06
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/bsVipManage" )
@Tag(description = "bsVipManage" , name = "会员管理管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BsVipManageController {

	private static final Logger log = LoggerFactory.getLogger(BsVipManageController.class);
	private final BsVipManageService bsVipManageService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param bsVipManage 会员管理
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
	@Inner(value = false)
    public R getBsVipManagePage(@ParameterObject Page page, @ParameterObject BsVipManageEntity bsVipManage) {
        LambdaQueryWrapper<BsVipManageEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(bsVipManageService.page(page, wrapper));
    }

	/**
	 * 分页查询
	 * @param bsVipManage 会员管理
	 * @return
	 */
	@Operation(summary = "分页查询" , description = "分页查询" )
	@GetMapping("/list" )
	@Inner(value = false)
	public R list( @ParameterObject BsVipManageEntity bsVipManage) {
		LambdaQueryWrapper<BsVipManageEntity> wrapper = Wrappers.lambdaQuery();
		return R.ok(bsVipManageService.list( wrapper));
	}

    /**
     * 通过id查询会员管理
     * @param vipId id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{vipId}" )
	@Inner(value = false)
    public R getById(@PathVariable("vipId" ) Long vipId) {
        return R.ok(bsVipManageService.getById(vipId));
    }

    /**
     * 新增会员管理
     * @param bsVipManage 会员管理
	 *                   admin 注册会员账号 绑定会员的
     * @return R
     */
    @Operation(summary = "新增会员管理" , description = "新增会员管理" )
    @SysLog("新增会员管理" )
    @PostMapping
	@Inner(value = false)
    public R save(@RequestBody BsVipManageEntity bsVipManage) {

        return R.ok(bsVipManageService.saveVip(bsVipManage));
    }

    /**
     * 修改会员管理
     * @param bsVipManage 会员管理
     * @return R
     */
    @Operation(summary = "修改会员管理" , description = "修改会员管理" )
    @SysLog("修改会员管理" )
    @PutMapping
	@Inner(value = false)
    public R updateById(@RequestBody BsVipManageEntity bsVipManage) {
        return R.ok(bsVipManageService.updateById(bsVipManage));
    }

    /**
     * 通过id删除会员管理
     * @param ids vipId列表
     * @return R
     */
    @Operation(summary = "通过id删除会员管理" , description = "通过id删除会员管理" )
    @SysLog("通过id删除会员管理" )
    @DeleteMapping
	@Inner(value = false)
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(bsVipManageService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param bsVipManage 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
	@Inner(value = false)
    public List<BsVipManageEntity> export(BsVipManageEntity bsVipManage,Long[] ids) {
        return bsVipManageService.list(Wrappers.lambdaQuery(bsVipManage).in(ArrayUtil.isNotEmpty(ids), BsVipManageEntity::getVipId, ids));
    }
}